package interceptor;

import cn.hutool.core.util.ObjectUtil;
import entity.user;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("preHandle方法在控制器的处理请求方法前执行");
        HttpSession session = request.getSession();
        user u = (user) session.getAttribute("u");
        System.out.println(u.getUserName());
        if(ObjectUtil.isEmpty(u)){
            response.getWriter().write("未登录");
        }else {
            return true;
        }

        return false;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle方法在控制器的处理请求方法调用之后，解析视图之前执行");
    }
}
